﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using PlanningGIS.Framework;

namespace PlanningGIS.TocContextMenu.Button
{
    /// <summary>
    /// 作用：TOC右键菜单设置当前比例尺为最小可视比例尺按钮类
    /// </summary>
    public class SetAsMinScale : BaseCommand 
    {
        /// <summary>
        /// 
        /// </summary>
        public SetAsMinScale()
        {
            base.m_caption = "设置当前比例尺为最小可视比例尺";
            base.m_toolTip = base.Caption;
        }

        /// <summary>
        /// 
        /// </summary>
        IApplication _Application = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="hook"></param>
        public override void OnCreate(object hook)
        {
            if (_Application == null)
                _Application = hook as IApplication;
        }

        /// <summary>
        /// 点击事件
        /// </summary>
        public override void OnClick()
        {
            if (_Application == null) return;
            ILayer pLayer = null;
            if ((_Application as IFramework).Object is IMapControl4 && ((_Application as IFramework).Object as IMapControl4).CustomProperty != null)
            {
                pLayer = ((_Application as IFramework).Object as IMapControl4).CustomProperty as ILayer;
                pLayer.MinimumScale = ((_Application as IFramework).Object as IMapControl4).MapScale;
            }
            if ((_Application as IFramework).Object is IPageLayoutControl2 && ((_Application as IFramework).Object as IPageLayoutControl2).CustomProperty != null)
            {
                pLayer = ((_Application as IFramework).Object as IPageLayoutControl2).CustomProperty as ILayer;
                pLayer.MinimumScale = ((_Application as IFramework).Object as IPageLayoutControl2).ActiveView.FocusMap.MapScale;
            }
            if (pLayer != null)
            {
                (_Application.Document as IMxDocument).ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, (_Application.Document as IMxDocument).ActiveView.Extent);
                (_Application as IFramework).TOCControl.Update();
            }
        }

        /// <summary>
        /// 是否可用
        /// </summary>
        public override bool Enabled
        {
            get
            {
                if (_Application == null) return false;
                if ((_Application as IFramework).Object is IMapControl4 && ((_Application as IFramework).Object as IMapControl4).CustomProperty!=null)
                {
                    return (((_Application as IFramework).Object as IMapControl4).CustomProperty as ILayer).Valid;
                }
                if ((_Application as IFramework).Object is IPageLayoutControl2 && ((_Application as IFramework).Object as IPageLayoutControl2).CustomProperty != null)
                {
                    return (((_Application as IFramework).Object as IPageLayoutControl2).CustomProperty as ILayer).Valid;
                }
                return false;
            }
        }

        /// <summary>
        /// 图标
        /// </summary>
        public override int Bitmap
        {
            get
            {
                string bitmapResourceName = "PlanningGIS.TocContextMenu.Res." + this.GetType().Name + ".ico";
                System.IO.Stream stream = GetType().Assembly.GetManifestResourceStream(bitmapResourceName);
                if (stream == null) return 0;
                return new Bitmap(stream).GetHbitmap().ToInt32();
            }
        }

    }
    
}
